import { login, getUser } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getMypermissions } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    getUser: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      // console.log(111, state.jwt)
      setToken(token)
    },
    logout(state) {
      state.token = null
      removeToken()
    },
    getUser(state, data) {
      // console.log(1, state, data)
      state.getUser = data
    }
  },
  actions: {
    async login(store, data) {
      const res = await login(data)
      // console.log('登入接口', res)
      store.commit('setToken', res.data.jwt)
    },

    async getUser(store, data) {
      const res = await getUser()
      // 获取当前用户的权限
      const result = await getMypermissions()
      // console.log(111, res)
      store.commit('getUser', { ...res, ...result.data })
    },
    removeToken(store) {
      store.commit('logout')
    }
  }
}
